Conditions | 1 |
Paths | 8 |
Total Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | |||
123 | function sendRequest(form) |
||
124 | { |
||
125 | $('#toggle-lang-response').trigger('click'); |
||
126 | |||
127 | var $form = $(form); |
||
128 | var $section = $form.closest('section'); |
||
129 | |||
130 | var $btn = $form.find('button[type="submit"]'); |
||
131 | $btn.html($('#preloader-template').html()).attr('disabled', true); |
||
132 | |||
133 | var headers = {}; |
||
134 | $section.find('.headers-form .form-group').not('.except').each(function(key, element) { |
||
135 | var $el = $(element); |
||
136 | if ($el.find('.req-header-active').is(':checked')) { |
||
137 | var header = $el.find('.req-header').val(); |
||
138 | headers[header] = $el.find('.req-header-value').val(); |
||
139 | } |
||
140 | }); |
||
141 | $('.global-headers-form .form-group').not('.except').each(function(key, element) { |
||
142 | var $el = $(element); |
||
143 | if ($el.find('.req-header-active').is(':checked')) { |
||
144 | var header = $el.find('.req-header').val(); |
||
145 | headers[header] = $el.find('.req-header-value').val(); |
||
146 | } |
||
147 | }); |
||
148 | |||
149 | $.ajax({ |
||
150 | url : $section.find('.action-url').val(), |
||
151 | headers: headers, |
||
152 | type : $form.attr('method'), |
||
153 | data : $form.serializeArray(), |
||
154 | success : function(response, status, xhr) { |
||
155 | $btn.text('Send').attr('disabled', false); |
||
156 | $section.find('.method-example-endpoint code.response-content.response-highlighted').jsonViewer(response); |
||
157 | $section.find('.method-example-endpoint code.response-content.response-raw').text(typeof response == 'object' ? JSON.stringify(response): String(response)); |
||
158 | $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders()); |
||
159 | }, |
||
160 | error : function(xhr) { |
||
161 | $btn.text('Send').attr('disabled', false); |
||
162 | var content = xhr.responseText; |
||
163 | if (xhr.statusText && !content) { |
||
164 | $.notify({ |
||
165 | message: xhr.statusText |
||
166 | },{ |
||
167 | type: 'danger' |
||
168 | }); |
||
169 | } |
||
170 | if (IsJsonString(content)) { |
||
171 | $section.find('.method-example-endpoint code.response-content').jsonViewer(content); |
||
172 | return; |
||
173 | } |
||
174 | var $frame = $('<iframe class="supa" style="width:100%; height:350px;">'); |
||
175 | $section.find('.method-example-endpoint code.response-content.response-highlighted').html($frame); |
||
176 | $section.find('.method-example-endpoint code.response-content.response-raw').text(typeof content == 'object' ? JSON.stringify(content): String(content)); |
||
177 | setTimeout(function() { |
||
178 | var doc = $frame[0].contentWindow.document; |
||
179 | var $body = $('body', doc); |
||
180 | $body.html(content); |
||
181 | }, 1); |
||
182 | |||
183 | $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders()); |
||
184 | } |
||
185 | }); |
||
186 | |||
187 | return false; |
||
188 | } |
||
189 | |||
247 |